<?php


namespace app\app\controller;


use think\Controller;
use think\Db;

class People extends Controller
{
    protected $peopleInfo;
    protected $peopleID;

    protected function initialize()
    {
        parent::initialize();
        $token = $_SERVER['HTTP_TOKEN'] ?? '';
        if (empty($token)) {
            responseJson(-100, '请先登录配送端');
        }
        //todo 使用明文openid 后面加密 解密
        $delivery = Db::name('delivery')->where('token', '=', $token)->find();
        if (!$delivery) {
            responseJson(-100, '登录信息有误');
        }
        $this->peopleInfo = $delivery;
        $this->peopleID = $delivery['id'];
    }

    //上线
    public function GoOnline()
    {
        Db::startTrans();
        try {
            $update = array(
                'id' => $this->peopleID,
                'is_online' => 1
            );
            $update = Db::name('delivery')->update($update);
            Db::commit();
            msg(0, '更新成功', $update);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }
    }

//425253fda969a0434afdd101a5ea584d
    //线下 退出
    public function offOnline()
    {
        Db::startTrans();
        try {
            $update = array(
                'id' => $this->peopleID,
                'is_online' => 0,
                //'token'=>null
            );
            $update = Db::name('delivery')->update($update);
            Db::commit();
            msg(0, '更新成功', $update);
        } catch (\Exception $e) {
            msg(-1, $e->getMessage(), '');
        }
    }

    //获取配送订单
    public function getOrder()
    {
        $page = input('page/d', 1);
        $length = input('limit/d', 10);
        $where[] = ['status', '=', 2]; // '0待支付 1待发货 2待收货 配送中 3已完成 4已取消 5售后',
        $where[] = ['delivery_id', '=', $this->peopleID];
        $where[] = ['write_status', '=', 0];  //'核销状态 0未核销 1已核销',
        //$where[] = ['is_tui', '=', 0];//没退款
        //  $order = Db::name('order')->where($where)->find();
        $business_info = Db::name('business')->find($this->peopleInfo['business_id']);
        $order = Db::name('order')->order('dispatch_time desc')->where($where)->paginate([
            $length, false, [
                'page' => $page
            ]
        ])->each(function ($item) use ($business_info) {
            //todo 单商品下单只要一个商品信息
            $item['business_info'] = $business_info;
            $item['deliver_address'] = Db::name('delivery_address')->find($item['address_id']);
            $order_detail = Db::name('order_detail')->where('order_id', $item['id'])->find();
            $item['order_detail'] = $order_detail;
            $item['goods_detail'] = Db::name('goods')->find($order_detail['goods_id']);
            return $item;
        });

        msg(0, '查询成功', $order);
        //order
        //`status` tinyint(2) DEFAULT NULL COMMENT '0待支付 1待发货 2待收货 配送中 3已完成 4已取消 5售后',
        //`dispatch_time` int(16) DEFAULT NULL COMMENT '派单时间',
    }

    //获取完成配送订单列表
    public function getFinishOrderList()
    {
        //不关送达后订单如何 只要有送达时间就算
        $where[] = ['write_status', '=', 1];
        $where[] = ['delivery_id', '=', $this->peopleID];
        $page = input('page/d', 1);
        $length = input('limit/d', 10);
        $business_info = Db::name('business')->find($this->peopleInfo['business_id']);
        $data_list = Db::name('order')
            ->order('deliver_time desc')
            ->where($where)->paginate([
                $length, false, [
                    'page' => $page
                ]
            ])->each(function ($item) use ($business_info) {
                //todo 单商品下单只要一个商品信息
                $item['business_info'] = $business_info;
                $item['deliver_address'] = Db::name('delivery_address')->find($item['address_id']);
                $order_detail = Db::name('order_detail')->where('order_id', $item['id'])->find();
                $item['order_detail'] = $order_detail;
                $item['goods_detail'] = Db::name('goods')->find($order_detail['goods_id']);
                return $item;
            });
        msg(0, '查询成功', $data_list);
    }


    /**
     * 获取配送人员信息
     */
    public function getPeopleInfo()
    {
        msg(1, '查询完成', $this->peopleInfo);
    }

    /**
     * 配送人员点击送达
     */
    public function deliverOrder()
    {
        $data = $this->request->param();
        //经纬度
        $order_id = $data['order_id'];
        $latitude = $data['latitude'];
        $longitude = $data['longitude'];
        //判断是否在附近
        $order_info = Db::name('order')
            ->where('delivery_id', '=', $this->peopleID)
            ->find($order_id);
        $address = Db::name('delivery_address')->find($order_info['address_id']);
        $from = $latitude . ',' . $longitude;
        $to = $from . ';' . $address['latitude'] . ',' . $address['longitude'];
        $distance = getDistance($from, $to);
        //todo 距离先固定以后在后台可设置 已完成默认1000
        $allow_distance = config('site.deliver_distance') ? config('site.deliver_distance') : 1000;
        if ($distance > $allow_distance) {
            msg(-1, '距离过远，请靠近确认', '');
        }
        $result = deliverGo($order_id, 1, 2, 0, $this->peopleID);
        if ($result == -1) {
            msg(-1, '操作有误', $result);
        }
        msg(0, '更新完成', $result);
    }

    //订单扫码核销
    public function verifyOrder()
    {
        $data = $this->request->param();
        $order_id = $data['order_id'];
        $result = deliverGo($order_id, 2, 2, 0, $this->peopleID);
        if ($result == -1) {
            msg(-1, '操作有误', $result);
        }
        msg(0, '更新完成', $result);
    }

    //核销码扫码核销
    public function verifyOrderDigits()
    {
        $data = $this->request->param();
        $order_id = $data['order_id'];
        $write_code_digits = $data['write_code_digits'];
        $result = deliverGo($order_id, 3, 2, 0, $this->peopleID, $write_code_digits);
        if ($result == -1) {
            msg(-1, '操作有误', $result);
        }
        msg(0, '更新完成', $result);
    }

    public function peopleData()
    {
        $timestamp = strtotime("today midnight");
        $y_tims = getTimestamp(-1);

        $num1 = Db::name('order')
            ->where('delivery_id', '=', $this->peopleID)
            ->where('write_status', 1)
            ->sum('price');
        $num2 = Db::name('order')
            ->where('delivery_id', $this->peopleID)
            ->where('deliver_time', 'between', [$y_tims[0], $y_tims[1]])
            ->where('write_status', 1)
            ->sum('price');

        $num3 = Db::name('order')
            ->where('delivery_id', $this->peopleID)
            ->where('write_status', 1)
            ->count();
        $num4 = Db::name('order')
            ->where('delivery_id', $this->peopleID)
            ->where('write_status', 1)
            ->where('deliver_time', 'between', [$y_tims[0], $y_tims[1]])
            ->count();
        //今日完成交易额
        $num5 = Db::name('order')
            ->where('delivery_id', $this->peopleID)
            ->where('write_status', 1)
            ->where('deliver_time', 'between', [$timestamp, time()])
            ->sum('price');
        //
        $num6 = Db::name('order')
            ->where('delivery_id', $this->peopleID)
            ->where('write_status', 1)
            ->where('deliver_time', 'between', [$timestamp, time()])
            ->count();

        $data = array(
            'del_price' => $num1,
            'y_del_price' => $num2,
            'order_num' => $num3,
            'y_order_num' => $num4,
            'dai_01' => $num5,
            'dai_02' => $num6
        );
        msg(0, '更新完成', $data);
    }


}